package socdev.data;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONException;
import org.json.JSONObject;

public class Project implements Serializable{

	public String name;
	public String key;
	public String github;
	public String description;
	public String role;
	
	public Project(String name, String key, String github, String description, String role) {
		this.name = name;
		this.key = key;
		this.github = github;
		this.description = description;
		this.role = role;
	}

	
	public static Project createFromJson(JSONObject json) throws JSONException {
		return new Project(json.getString("name"), json.getString("key"), json.getString("github"), json.getString("description"), json.getString("role"));
	}
	
	public String getId() {
		return key;
	}
	
	public boolean isMember() {
		return !this.role.equals("none");
	}
	public List<NameValuePair> getNameValuePairs(List<NameValuePair> nameValuePairs) {
		 
		
		nameValuePairs.add(new BasicNameValuePair("group_key",key));
     	return nameValuePairs;
		
	}
	
	public List<NameValuePair> getNameValuePairsKey(List<NameValuePair> nameValuePairs) {
		 
		
		nameValuePairs.add(new BasicNameValuePair("key",key));
     	return nameValuePairs;
		
	}
	
	public boolean isGithub() {
		return github != null && !github.isEmpty();

	}
	
	
	public static void join(User user, String name) {
		
		Thread push = new Thread(new Join(user, name));
		push.start();
	}

	public static class Join implements Runnable {
		User user;
		String name;
		
		public Join(User user, String name) {
			this.user = user;
			this.name = name;
		}
		
		@Override
		public void run() {
			try {
				List<NameValuePair> namesP = new ArrayList<NameValuePair>();
				namesP.add(new BasicNameValuePair("name",name));
				JSonDownloader.postData("http://socdev-dev1.appspot.com/api/group/join/", (user.getNameValuePairs(namesP)));
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
		}
	}
}
